package its.opt.utils;

import its.opt.structures.BOAParams;
import its.opt.structures.EvolutionParams;
import its.sim.SimulatorConfiguration;
import its.sim.ui.SimulatorFiles;

import java.io.File;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

public final class LoggingUtils {

	public static String makeLogStringForAlgorithmParameters(
			String[] args,
			SimulatorConfiguration configuation)
	{
		StringBuilder sb = new StringBuilder();
		
		
		sb.append("Application arguments: ");
		for (String arg : args) {
			sb.append(arg);
			sb.append(" ");
		}
		sb.append('\n');
		sb.append("Application configuration: ");
		
		File configFile = configuation.getConfigFile();
		SimulatorFiles simulatorFiles = null;
		try {
			simulatorFiles = new SimulatorFiles(configFile);
		} catch (Exception e) {}
		
		if (simulatorFiles != null) {
			sb.append("\nEnvironment file: ");
			sb.append(simulatorFiles.getEnvironmentFile().toString());
			sb.append("\nGenerator file: ");
			sb.append(simulatorFiles.getGeneratorFile().toString());
			sb.append("\nShapes file: ");
			sb.append(simulatorFiles.getShapesFile().toString());
		} else {
			sb.append("!MISSING CONFIGURATION!");
		}
		
		sb.append("\n-- listing properties --\n");
		Properties properties = System.getProperties();
		Set<Entry<Object, Object>> entrySet = properties.entrySet();
		for (Entry<Object, Object> entry : entrySet) {
			String key = (String) entry.getKey();
			
			if (key.equals("population.size")
					|| key.equals("max.generations")
					|| key.equals("percent.offspring")
					|| key.equals("mutation.probability")
					|| key.equals("tournament.size")
					|| key.equals("max.parents"))
			{
				String val = (String) entry.getValue();
				sb.append(key);
				sb.append("=");
				sb.append(val);
				sb.append("\n");
			}
		}
		return sb.toString();
	}

	public static String makeLogStringForAlgorithmParameters(BOAParams boaParams) {
		StringBuilder sb = new StringBuilder();
		String s = makeLogStringForAlgorithmParameters((EvolutionParams) boaParams);
		sb.append(s);
		sb.append("Max incoming edges (parents): ");
		sb.append(boaParams.maxIncoming);
		sb.append('\n');
		sb.append("Is meging nodes allowed: ");
		sb.append(boaParams.allowMerge);
		sb.append('\n');
		return sb.toString();
	}
	
	public static String makeLogStringForAlgorithmParameters(EvolutionParams params) {
		StringBuilder sb = new StringBuilder();
		sb.append("Max generations: ");
		sb.append(params.maxGenerations);
		sb.append('\n');
		sb.append("Population size: ");
		sb.append(params.N);
		sb.append('\n');
		sb.append("Chromosome size: ");
		sb.append(params.n);
		sb.append('\n');
		sb.append("Mutation probability: ");
		sb.append(params.mutationProbability);
		sb.append('\n');
		sb.append("Percent offspring: ");
		if (params.percentOffspring != 0) {
			sb.append(params.percentOffspring);
		} else {
			sb.append("unused");
		}
		sb.append('\n');
		sb.append("Tournament size: ");
		if (params.tournamentSize != 0) {
			sb.append(params.tournamentSize);
		} else {
			sb.append("unused");
		}
		sb.append('\n');
		return sb.toString();
	}
}
